﻿CREATE PROCEDURE [dbo].[GoodsSearchEx]
(
@DocId int,
@Barcode varchar(12),
@Name varchar(150),
@DepartmentId int)
AS
/* Отпуск товара - поиск в справочниках*/ 
/* По штрих коду*/ 
IF @Barcode IS NOT NULL

SELECT ReturnLineId = CONVERT(int, NULL), PartyId = p.Id, Barcode = p.barcode, 
Name = v.FullName, ManufacturerName = v.ManufacturerName, CountryName=v.CountryName,
RetailPrice = p.RetailPrice, Qntty = p.RestQntty
FROM cd.Parties p JOIN ca.Goods g ON g.Id = p.GoodsId INNER JOIN
dbo.vwGoodsDetailed v ON v.Id = p.GoodsId
WHERE     p.barcode = @Barcode AND p.blocked = 0
AND p.DepartmentId = @DepartmentId
ORDER BY v.FullName, v.ManufacturerName 

ELSE/* По наименованию*/ 
SELECT ReturnLineId = CONVERT(int,NULL), PartyId = p.Id,Barcode = p.barcode,
Name = v.FullName, ManufacturerName = v.ManufacturerName, CountryName=v.CountryName,
RetailPrice = p.RetailPrice, Qntty = p.RestQntty
FROM  ca.Assortment a INNER JOIN ca.Goods g 
ON g.AssortmentId = a.Id INNER JOIN dbo.vwGoodsDetailed v 
ON v.Id = g.Id LEFT OUTER JOIN cd.Parties p 
ON p.GoodsId = g.Id AND p.blocked = 0
WHERE a.Name LIKE @Name AND p.DepartmentId = @DepartmentId
ORDER BY v.FullName, v.ManufacturerName
